package com.kkd.kkdapp.adapter;

import java.util.List;

import com.kkd.kkdapp.R;
import com.kkd.kkdapp.http.entity.Privilege;
import com.nostra13.universalimageloader.core.ImageLoader;

import android.content.Context;
import android.graphics.Color;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class PrivilegeViewPagerAdapter extends PagerAdapter {

	private Context context;
	private List<Privilege> arr;

	public PrivilegeViewPagerAdapter(Context context, List<Privilege> arr) {
		this.context = context;
		this.arr = arr;
	}

	@Override
	public int getCount() {
		return arr.size();
	}

	@Override
	public boolean isViewFromObject(View arg0, Object arg1) {
		return arg0 == arg1;
	}

	public void destroyItem(android.view.ViewGroup container, int position,
			Object object) {
		container.removeView((View) object);
	}

	@Override
	public Object instantiateItem(ViewGroup container, final int position) {
		View view = View.inflate(context, R.layout.privilegeviewpager_item,
				null);
		TextView privilege_tv = (TextView) view.findViewById(R.id.privilege_tv);
		RelativeLayout privilege_bt = (RelativeLayout) view
				.findViewById(R.id.privilege_bt);
		int status = arr.get(position).getStatus();
		if (status == 0) {
			privilege_tv.setText("投资拿特权");
			privilege_tv.setTextColor(Color.parseColor("#5451b1"));
			privilege_bt.setClickable(true);
			privilege_bt.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					if (callback != null) {
						callback.clickItem(position);
					}
				}
			});
		} else if (status == 1) {
			privilege_tv.setText("已拥有");
			privilege_tv.setTextColor(Color.parseColor("#999999"));
			privilege_bt.setClickable(false);
		}
		ImageView img = (ImageView) view.findViewById(R.id.img);
		ImageLoader.getInstance().displayImage(
				arr.get(position).getImage_big(), img);
		container.addView(view);
		return view;
	}

	OnViewPagerItemListener callback;

	public void setOnViewPagerItemListener(OnViewPagerItemListener callback) {
		this.callback = callback;
	}

	public interface OnViewPagerItemListener {
		public void clickItem(int position);
	}

}
